package com.sftz.modules.minapp.myExam.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nutz.dao.Cnd;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.Param;

import com.sftz.assets.entities.question.PaperAnswerInfoEntity;
import com.sftz.assets.enums.AnswerStatusEnum;
import com.sftz.assets.enums.StateOrTimeEnum;
import com.sftz.assets.enums.YesOrNoEnum;
import com.sftz.modules.minapp.myExam.dto.MyPaperDto;
import com.sftz.modules.minapp.myExam.form.MyPaperShowForm;
import com.sftz.modules.minapp.resourceRecord.dto.PersonCourseScoreDto;
import com.sftz.modules.minapp.resourceRecord.service.StudentOtherCourseScoreViewService;
import com.uxuexi.core.common.util.Util;
import com.uxuexi.core.db.util.DbSqlUtil;
import com.uxuexi.core.web.base.service.BaseService;

@IocBean
public class MyPagerViewService extends BaseService<PaperAnswerInfoEntity> {
	private static final Log log = Logs.get();

	@Inject
	private StudentOtherCourseScoreViewService studentOtherCourseScoreViewService;

	/**
	 * 
	 * @author   赵硕
	 * @Date	 2018年12月20日
	 *（用途:我的考试）全部只针对审批和未审批的试卷，不包括已过期和将过期的
	 */
	public Object list(Integer userId, Integer courseId, MyPaperShowForm myPaperShowForm) {
		Map<String, Object> obj = new HashMap<String, Object>();
		if (Util.isEmpty(userId)) {
			return null;
		} else {
			Integer courseStatus = myPaperShowForm.getCourseStatus();
			List<MyPaperDto> myPaper = new ArrayList<MyPaperDto>();
			if (courseStatus == StateOrTimeEnum.ALLSTA.intKey()) {
				//已审批、未审批数据
				myPaper = corretOrNotmyExamList(userId, courseId, myPaperShowForm);
				//将过期
				List<MyPaperDto> willDatedList = willDatedList(userId, courseId);
				//已过期
				List<MyPaperDto> datedList = datedList(userId, courseId);
				myPaper.addAll(willDatedList);
				myPaper.addAll(datedList);
			} else if (courseStatus == StateOrTimeEnum.APPROVED.intKey()
					|| courseStatus == StateOrTimeEnum.UNAPPROVED.intKey()) {
				//已审批或者未审批
				myPaper = corretOrNotmyExamList(userId, courseId, myPaperShowForm);
			} else if (courseStatus == StateOrTimeEnum.WILLDATED.intKey()) {
				//将过期
				myPaper = willDatedList(userId, courseId);
			} else if (courseStatus == StateOrTimeEnum.DATED.intKey()) {
				//已过期
				myPaper = datedList(userId, courseId);
			}
			obj.put("myPaper", myPaper);
			return obj;
		}
	}

	//（用途:我的考试记录进度查询）
	public PersonCourseScoreDto myExamProcessData(@Param("userId") final Integer userId,
			@Param("courseId") final Integer courseId) {
		//根据登录Id查询当前用户属于哪个机构
		return studentOtherCourseScoreViewService.findStudentScore(courseId, userId);
	}

	//未审批-已审批的时间查询	
	public List<MyPaperDto> corretOrNotmyExamList(Integer userId, Integer courseId, MyPaperShowForm myPaperShowForm) {
		Integer courseStatus = myPaperShowForm.getCourseStatus();
		//全部、已审批、未审批SQL语句
		Sql studentS = Sqls.create(sqlManager.get("minapp_myExam_list"));
		Cnd cnd = Cnd.NEW();

		//起始时间
		//全部-未审批-已审批的时间查询

		if (courseStatus == StateOrTimeEnum.APPROVED.intKey() || courseStatus == StateOrTimeEnum.UNAPPROVED.intKey()) {
			cnd.and("hi.status", "=", myPaperShowForm.getCourseStatus());
		} else {
			cnd.and("hi.status", "!=", AnswerStatusEnum.start.intKey()).and("hi.status", "!=",
					AnswerStatusEnum.uncommit.intKey());
		}

		cnd.and("hi.studentId", "=", userId);
		cnd.and("h.courseId", "=", courseId);
		studentS.setCondition(cnd);
		List<MyPaperDto> myPaper = DbSqlUtil.query(dbDao, MyPaperDto.class, studentS);
		return myPaper;

	}

	//将过期数据查询
	public List<MyPaperDto> willDatedList(Integer userId, Integer courseId) {
		//将过期数据SQL语句
		Sql studentS = Sqls.create(sqlManager.get("minapp_myExam_willOrNotWill_list"));
		Cnd cnd = Cnd.NEW();
		cnd.and("p.useStatus", "=", YesOrNoEnum.YES.intKey());

		String dateNow = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());

		Calendar calendar = Calendar.getInstance();
		calendar.add(calendar.HOUR, 48);
		String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
		SqlExpressionGroup group = new SqlExpressionGroup();
		group.andBetween("p.endTime", dateNow, date).orBetween("postDate", dateNow, date);
		cnd.and(group);
		//cnd.and("p.endTime", ">", dateNow).and("p.endTime", "<=", date);

		studentS.setParam("userId", userId);
		studentS.setParam("status", AnswerStatusEnum.start.intKey());
		studentS.setParam("isDelete", YesOrNoEnum.NO.intKey());
		studentS.setParam("courseId", courseId);
		studentS.setCondition(cnd);
		List<MyPaperDto> myPaper = DbSqlUtil.query(dbDao, MyPaperDto.class, studentS);
		if (!Util.isEmpty(myPaper)) {
			for (MyPaperDto myPaperDto : myPaper) {
				myPaperDto.setStatus(StateOrTimeEnum.WILLDATED.intKey());
			}
		}
		return myPaper;

	}

	//已过期数据查询
	public List<MyPaperDto> datedList(Integer userId, Integer courseId) {
		//将过期数据SQL语句
		Sql studentS = Sqls.create(sqlManager.get("minapp_myExam_willOrNotWill_list"));
		Cnd cnd = Cnd.NEW();
		cnd.and("p.useStatus", "=", YesOrNoEnum.YES.intKey());

		String dateNow = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		cnd.and("p.endTime", "<", dateNow);

		studentS.setParam("userId", userId);
		studentS.setParam("status", AnswerStatusEnum.start.intKey());
		studentS.setParam("isDelete", YesOrNoEnum.NO.intKey());
		studentS.setParam("courseId", courseId);
		studentS.setCondition(cnd);
		List<MyPaperDto> myPaper = DbSqlUtil.query(dbDao, MyPaperDto.class, studentS);
		if (!Util.isEmpty(myPaper)) {
			for (MyPaperDto myPaperDto : myPaper) {
				myPaperDto.setStatus(StateOrTimeEnum.DATED.intKey());
			}
		}
		return myPaper;

	}

}
